﻿using System;
using System.Collections.Generic;
using System.Linq;
using Way.EAP.DataAccess.Entity;
using Way.EAP.DataAccess.Regulation;
using Way.Bus.WMS.Controllers;
using Way.Contract.MVC;
using Way.Bus.WMS.Model;
using Way.Contract.Data.ControllerData;
using InterfaceService.DataAccess;
using System.Data;
using System.Configuration;
using Way.Bus.Model;
using Way.Bus.WMS.SF.Model;

namespace Way.Bus.WMS.SF.Controllers
{
    public class SFUnPurchaseOrderController : StockOutOrderController
    {
        /// <summary>
        /// 描述：圣沣返工出库控制器
        /// 作者：路聪
        /// 创建日期:2016年8月21日22:00:44
        /// </summary>


        #region 初始化数据方法
        #region 基础设置
        //是否启用审核--未启用因为审核人不可以为当前登陆人
        public override bool IsReviewedState
        {
            get
            {
                return true;
            }
        }
        //是否启用提交
        public override bool IsSubmit
        {
            get
            {
                return true;
            }
        }
        //是否启用回收站
        public override bool IsClearAway
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 是否启用下推
        /// </summary>
        public override bool IsPushDown
        {
            get
            {
                return false;
            }
        }

        //显示在菜单
        protected override string OnControllerName()
        {
            return "返工出库";
        }
        //菜单中url路径
        protected override string OnGetPath()
        {
            return "Views/SF/SFUnPurchaseOrder.htm";
        }
        //开发人
        protected override string OnGetAuthor()
        {
            return "路聪";
        }
        //说明
        protected override string OnGetControllerDescription()
        {
            return "返工出库";
        }
        #endregion

        /// <summary>
        /// 初始化显示与否
        /// </summary>
        /// <param name="data"></param>
        /// <param name="item"></param>
        protected override void OnInitViewChildItem(Contract.Data.ModelData data, Contract.Data.ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (data.name.EndsWith("StockOutOrder"))
            {
                data.title = "返工出库";
                switch (item.field.ToLower())
                {
                    case "code":
                        item.visible = true;
                        item.index = 1;
                        break;
                    case "clientno":
                        item.visible = true;
                        item.title = "客户名称";
                        item.index = 3;
                        break;
                    case "towhno":
                        item.visible = true;
                        item.title = "出库仓库";
                        item.index = 4;
                        item.required = true;
                        break;
                    case "workerid":
                        item.visible = true;
                        item.title = "销售人员";
                        item.required = true;
                        item.index = 5;
                        break;
                    case "inouttime":
                        item.visible = true;
                        item.index = 6;
                        item.required = true;
                        break;
                    case "fregionid":
                        item.visible = true;
                        item.index = 8;
                        item.required = true;
                        break;
                    case "remark":
                        item.visible = true;
                        item.index = 9;
                        break;
                    case "createdby":
                    case "creationdate":
                    case "issubmited":
                    case "submitedby":
                    case "submiteddate":
                    case "isreviewed":
                    case "reviewedby":
                    case "revieweddate":
                        item.visible = true;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }
            if (data.name.EndsWith("StockOutOrderMaterials"))
            {
                data.title = "返工出库明细";

                //根据产品带出编码和规格
                if (item.field.ToLower() == "materialcode")
                {
                    if (item.foreign != null)
                    {
                        item.foreign.rowdisplay.Add("CODE", "MCODE");
                        item.foreign.rowdisplay.Add("FMODEL", "FMODEL");
                    }
                }
                switch (item.field.ToLower())
                {
                    case "materialcode":
                        item.index = 1;
                        item.visible = true;
                        break;
                    case "mcode":
                        item.visible = true;
                        item.disabled = true;
                        item.index = 2;
                        break;
                    case "fmodel":
                        item.visible = true;
                        item.disabled = true;
                        item.index = 3;
                        break;
                    case "num":
                        item.visible = true;
                        item.index = 4;
                        item.required = true;
                        break;
                    case "price":
                        item.index = 6;
                        item.visible = true;
                        break;
                    case "remark":
                        item.visible = true;
                        break;
                    default:
                        item.visible = false;
                        break;
                }
            }

        }
        #endregion


        public override void MergerOrderMaterials(StockOutOrder order)
        {
        }
        protected override void ValidateStockNum(StockOutOrderMaterials item, int orderid, int stockid)
        {
            if (string.IsNullOrEmpty(item.DEPOTWBS))
                item.DEPOTWBS = "0";
        }
        protected override int StockType()
        {
            return 2;
        }



        protected override void OnForeignLoading(IModel model, loadItem item)
        {
            ///选择产品的(圣沣中使用原料没有成品)
            if (this.fdata.filedname == "MATERIALCODE")
            {
                item.rowsql = "select * from (" + item.rowsql + ") a where a.CommodityType=0";
            }
            ///选择仓库
            if (this.fdata.filedname == "TOWHNO")
            {
                SQLWhere tempSql = new SQLWhere();
                tempSql.ColumnName = "Acc_WMS_WareHouse.WHTYPE";
                tempSql.Relation = "and";
                tempSql.Type = "int";
                tempSql.Value = "0";
                List<SQLWhere> tempList = new List<SQLWhere>();
                tempList.Add(tempSql);
                item.whereList = tempList.ToArray();
            }
            base.OnForeignLoading(model, item);
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="info"></param>
        protected override void OnReviewedData(BasicInfo info)
        {
            // UpdateSourceOrder(info);
            SFStockOutOrder ord = info as SFStockOutOrder;
            string Dwh = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).Code;
            if (Dwh.Length > 4)
                throw new Exception("异常：所选择客户的编码长度大于4，请修改客户编码！");
            base.OnReviewedData(info);
            UpdateOldDB(ord);
        }


        public override void OutWarehouse(Way.Bus.Model.BasicInfo info)
        {
            //base.OutWarehouse(info);
        }
        private void UpdateOldDB(SFStockOutOrder info)
        {
            EntityList<SFStockOutOrder> listSFoutorder = new EntityList<SFStockOutOrder>(this.model.GetDataAction());
            listSFoutorder.GetData(" ID=" + info.ID);
            SFStockOutOrder ord = listSFoutorder[0];


            ord.Materials.DataAction = this.model.GetDataAction();
            ord.Materials.GetData();
            string strSql = "";
            string strCon = "";
            string Dwh = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).ID.ToString().PadLeft(4, '0');
            string Dwmc = ord.GetForeignObject<BusinessCustomer>(this.model.GetDataAction()).CUSTOMERNAME;

            DateTime dt = ord.INOUTTIME;
            int dtYear = dt.Year;
            int dtMonth = dt.Month;
            int dtDay = dt.Day;
            string Rq = dtYear + "," + dtMonth + "," + dtDay;
            string Xh;
            string Bm;
            string Pm;
            string Gg;

            string Ywyh = "";
            string Ywy = "";
            string Qyh = "";
            string Qymc = "";
            string TempQyh = "";
            string TempQymc = "";
            string Fhr = "";
            string Iksl = "";
            string Je = "";
            Xh = "";
            string Ikdh = "";
            string Dj = "";
            if (ord.WORKERID.ToString() != "0")
            {
                EntityList<OfficeWorker> listrn = new EntityList<OfficeWorker>(this.model.GetDataAction());
                listrn.GetData(" ID=" + ord.WORKERID);
                Ywyh = listrn[0].ID.ToString().PadLeft(3, '0');
                Ywy = listrn[0].WorkName;
            }
            if (ord.FREGIONID.ToString() != "0")
            {
                EntityList<Region> listrn = new EntityList<Region>(this.model.GetDataAction());
                listrn.GetData(" ID=" + ord.FREGIONID);
                TempQyh = listrn[0].ID.ToString().PadLeft(3, '0');
                TempQymc = listrn[0].RegionName;
            }
            Fhr = Rq;
            Pm = "鸭产品";
            Ikdh = ord.Code;
            Xh = Ikdh;
            string strOldSql = "";
            DataTable dtOldIpkc = null;
            strCon = ConfigurationManager.AppSettings["FOXXG"].ToString() + "XUSJK.DBF" + ConfigurationManager.AppSettings["ENDCONN"].ToString();
            for (int i = 0; i < ord.Materials.Count; i++)
            {
                Bm = ord.Materials[i].GetForeignObject<Materials>(this.model.GetDataAction()).Code;
                Gg = ord.Materials[i].GetForeignObject<Materials>(this.model.GetDataAction()).FNAME;
                Iksl = ord.Materials[i].NUM.ToString();
                Dj = ord.Materials[i].PRICE.ToString();
                Je = (ord.Materials[i].PRICE * Convert.ToDouble(Iksl)).ToString();
                strOldSql = "SELECT * from Xusjk where Bm='" + Bm.Substring(3, 6) + "' and Xh='" + Xh + "'";
                dtOldIpkc = BusFoxProHelper.ExecuteDataTable(strCon, CommandType.Text, strOldSql);
                if (ord.Materials[i].Remark == "鲜品")
                {
                    Qyh = this.model.GetDataAction().GetValue("select ID from Acc_Bus_Region where RegionName='鲜品'").ToString().PadLeft(3, '0');
                    Qymc = "鲜品";
                }
                else
                {
                    Qyh = TempQyh;
                    Qymc = TempQymc;
                }
                if (dtOldIpkc.Rows.Count > 0)
                {
                    strSql = "UPDATE Xusjk set Sl=" + Iksl + ",Dj=" + Dj + ",Je=" + Je + ",Dwh='" + Dwh + "', Dwmc='" + Dwmc + "',Hkje=" + Je + ",Ywyh='" + Ywyh + "',Ywy='" + Ywy + "',Qyh='" + Qyh + "',Qymc='" + Qymc + "' where Bm='" + Bm.Substring(3, 6) + "' and Xh='" + Xh + "'";
                }
                else
                {
                    strSql = "insert into Xusjk(Xh,Bm,Ywyh,Ywy,Qyh,Qymc,Fhr,Dwh,Dwmc,Pm,Gg,Sl,Dj,Je,Fph,Til,Pvh,Mbvl,Xuti,Xufy,Jsfu,Shr,Hkr,Hkje,Qkje,Hhqk,Nbjg,Djie,Jgie,Bzvu,Jxrq,Xdju,Lxvi,Lxbl,Thyy,Uirq,Qyfz,Ixb1,Ixb2,Ixb3)  ";
                    strSql = strSql + "values('" + Xh + "','" + Bm.Substring(3, 6) + "','" + Ywyh + "','" + Ywy + "','" + Qyh + "','" + Qymc + "',date(" + Fhr + "),'" + Dwh + "','" + Dwmc + "','" + Pm + "','" + Gg + "'," + Iksl + "," + Dj + "," + Je + ",'" + Xh + "',0,'',0,0,0,'','',date(" + Fhr + ")," + Je + ",0,0,0,0,0,'',date(" + Fhr + "),0,0,0,'',date(" + Fhr + "),'',0,0,'')";
                }
                BusFoxProHelper.ExecuteNonQuery(strCon, CommandType.Text, strSql);
            }
        }
    }
}
